package org.lzy.shop.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.lzy.shop.base.BaseViewModel
import org.lzy.shop.response.ListDTO
import org.lzy.shop.viewmodel.repository.CommentRepository
import org.lzy.shop.viewmodel.repository.lmpl.CommentRepositoryImpl

/**
 * 用户评价的VM层
 */
class CommentModel : BaseViewModel() {

    private val commentRepository: CommentRepository = CommentRepositoryImpl()
    val getCommentData = MutableLiveData<MutableList<ListDTO>?>()

    fun getComment(page: Int?, goodsId: String?): LiveData<MutableList<ListDTO>?> {
        viewModelScope.launch {
            try {
                val result = commentRepository.getGoodsComment(page, goodsId)

                // 处理响应结果
                if (result != null) {
                    getCommentData.value = result
                } else {
                    // 错误处理
                    getCommentData.value = null
                    // 可以在这里添加错误提示逻辑
                }
            } catch (e: Exception) {
                // 异常处理
                e.printStackTrace()
                getCommentData.value = null
                // 可以在这里添加异常提示逻辑
            }
        }
        return getCommentData
    }}